package com.siteseat.crudeq.broker.v1.request;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;

import com.siteseat.common.foundation.Component;
import com.siteseat.crudeq.broker.netty.RequestHandler;
import com.siteseat.crudeq.broker.transport.BrokerSession;
import com.siteseat.crudeq.common.raw.protocol.v1.ErrorCode;

/**
 *
 */
public abstract class AbstractRequestHandler extends Component implements RequestHandler {

  /**
   * {@inheritDoc}
   */
  @Override
  protected void doInit() throws Exception {
    // do nothing so far
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected void doDestroy() throws Exception {
    // do nothing so far
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public boolean needAuthentication( BrokerSession session, Channel ch, ChannelBuffer input, byte reqId )
          throws Exception {
    return true;
  }

  /**
   * @param sess
   * @param errorCode
   * @param detail
   */
  protected void logError( BrokerSession sess, ErrorCode errorCode, String detail ) {
    String msg = "session=" + sess.getInternalId() + ", error code=" + errorCode + ", detail=" + detail;
    this.log.error( msg );
  }

  /**
   * @param sess
   * @param errorCode
   * @param detail
   */
  protected void logError( BrokerSession sess, ErrorCode errorCode, String detail, Exception cause ) {
    String msg = "session=" + sess.getInternalId() + ", error code=" + errorCode + ", detail=" + detail;
    this.log.error( msg, cause );
  }

}
